import router from '../../router'
import qs from 'qs'
var root=""
if (process.env.NODE_ENV === "development") {
  root='/api/'
}else {
  root='http://124.223.46.216/wjhapi/'
}

import axios from 'axios'
// import qs from 'qs'
// const baseUrl='' //就是url最前面都一样的
// let fetch=({methods,url,data,success,error})=>{ //methods 请求方法 get|post;  url 除了baseUrl之外的; data:传输的数据; success:请求成功之后要做什么
//   return new Promise((resolve,reject)=>{
//     if(methods.toUpperCase()==='get'){
//       axios.get(baseUrl+url,qs.stringify(data)).then(res=>{
//         success(res)
//         resolve(res)
//       })
//     }else{
//       axios.post(baseUrl+url,qs.stringify(data)).then(res=>{
//         success(res)
//         resolve(res)
//       })
//     }
//   })
// }

let req={
    get:(url,data,success)=>{
        let xhr=new XMLHttpRequest()
        let query="?user_token="+sessionStorage.getItem('user_token')+'&'
        let data_key=Object.keys(data)
        data_key.forEach((item)=>{
            query=query+item+'='+data[item]+'&'
        })
        console.log(query)
        xhr.open('GET',root+url+query,true)
        //"Content-type","application/x-www-form-urlencoded"
        xhr.setRequestHeader('Content-type','application/json;charset=utf-8')
        xhr.onreadystatechange=()=>{
            if(xhr.readyState==4&&xhr.status==200){
                let resopnse=JSON.parse(xhr.responseText)
                if(resopnse.status==401){
                  router.push('login')
                }
                success(resopnse)
            }
        }
        xhr.send()
    },
    fetch:(methods,url,data,success,error)=>{ //methods 请求方法 get|post;  url 除了baseUrl之外的; data:传输的数据; success:请求成功之后要做什么
      const baseUrl='http://39.170.55.25:10113/api/'

      return new Promise((resolve,reject)=>{
        if(methods.toLowerCase()==='get'){
          axios.get(baseUrl+url,qs.stringify(data),{
            headers: {
                'Authorization': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImFkbWluIiwiaWF0IjoxNjQ0NDU1ODI5LCJleHAiOjE2NDQ0OTkwMjl9.thX2hAnIF3E_VIPT80XA_XwrRo8Rpl77gDxklA3j_-g|admin'
            }
        }).then(res=>{
            success(res)
            resolve(res)
          })
        }else{
          axios.post(baseUrl+url,qs.stringify(data),{
            headers: {
                'Authorization': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImFkbWluIiwiaWF0IjoxNjQ0NDU1ODI5LCJleHAiOjE2NDQ0OTkwMjl9.thX2hAnIF3E_VIPT80XA_XwrRo8Rpl77gDxklA3j_-g|admin'
            }
        }).then(res=>{
            success(res)
            resolve(res)
          })
        }
      })
    },
    showmodal:(title,content,success)=>{
        
    },
    post:(url,data,success,err)=>{
        let xhr=new XMLHttpRequest()
        xhr.open('post',root+url,true)
        //"Content-type","application/x-www-form-urlencoded"
        xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded")
        xhr.onreadystatechange=()=>{
            if(xhr.readyState==4&&xhr.status==200){
                let resopnse=JSON.parse(xhr.responseText)
                console.log(resopnse);
                if(resopnse.status==401){
                  router.push('login')
                }
                if(resopnse.status==200){
                  
                    success(resopnse)
                }else{
                    alert(resopnse.message)
                    err(resopnse)||''
                }
            }else if(xhr.readyState==4&&xhr.status==500){
                err()
                alert('网络出错')
            }
        }
        data={...data,user_token:sessionStorage.getItem('user_token')}
        xhr.send(qs.stringify(data))
    },
    upload:(file,success)=>{
        let form=new FormData
        form.append('img',file)
        let xhr=new XMLHttpRequest()
        xhr.open('post',root+'upload',true)
        //"Content-type","application/x-www-form-urlencoded"
        //xhr.setRequestHeader("Content-type","multipart/form-data")
        xhr.onreadystatechange=()=>{
            if(xhr.readyState==4&&xhr.status==200){
                let resopnse=JSON.parse(xhr.responseText)
                success(resopnse)
            }
        }
        xhr.send(form)
    },
    test_page(res,page,list){
        try{
          if(res.data==undefined){
            return {data:list,page:page}
          }
          if(res.status==200&&page==1&&res.data.length!=0){
              list=res.data
              page++
              console.log('有数据覆盖')
              return {data:list,page:page}
            }else if(res.status!=200){
              this.$test_res()
              console.log('无数据')
              return {data:list,page:page}
            }else if(res.status==200&&res.data.length==0){
              console.log('无数据')
              return {data:list,page:page}
            }else if(res.status==200&&res.data.length!=0){
              let newlist=list.concat(res.data)
              console.log(newlist)
              page++
              console.log('有数据++')
              return {data:newlist,page:page}
          } else {
            this.$test_res(res)
            return {data:list,page:page}
          }
        }catch(e){
          uni.showModal({
            title: '提示',
            content: res.message,
            showCancel: false,
          });
        }
        
      }
    
}
export {req}